<?php

namespace backend\models\article;
use backend\models\article\Article;
use backend\models\user\User;
use yii\data\Pagination;
use Yii;

/**
 * This is the model class for table "{{%edu_comment}}".
 *
 * @property integer $comment_id
 * @property integer $comment_type
 * @property integer $model_id
 * @property integer $comment_addtime
 * @property integer $user_id
 * @property string $user_name
 * @property string $comment_content
 * @property integer $comment_status
 */
class Comment extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'edu_comment';
    }

    /**
     * @inheritdoc
     */
    
    public function rules()
    {
        return [
            [['comment_type', 'model_id', 'comment_addtime', 'user_id', 'comment_status'], 'integer'],
            [['comment_content'], 'string'],
			[ 'comment_addtime','default', 'value' =>time(),],
            [['user_name'], 'string', 'max' => 20]
        ];
    }

    
	/**
	* 资讯的两表联查
	**/
	public function getComment()
    {
		return $this->hasMany(Article::className(), ['article_id'=>'model_id']);
	}

	//总条数
	public function CommentCount()
    {
		return $this->find()
					->joinwith('comment')
                    ->where("comment_type=1")
					->count();
	}
	/**
	*  资讯查询
	*/
	public function CommentInfo($pagination)
    {
		return $this->find()
				->joinwith('comment')
				->where("comment_type=1")
				->offset($pagination->offset)
				->limit($pagination->limit)
				->asArray()
				->all();
	}

    //搜索总条数
    public function ComCount($code)
    {
        return $this->find()
                    ->joinwith('comment')
                    ->where("comment_type=1 $code")
                    ->count();
    }
    /**
    *  搜索资讯查询
    */
    public function ComSearch($code,$pagination)
    {
        return $this->find()
                ->joinwith('comment')
                ->where("comment_type=1 $code")
                ->offset($pagination->offset)
                ->limit($pagination->limit)
                ->asArray()
                ->all();
    }
	/**
	* 删除评论
	*/
	public function CommentDelete($comment_id)
    {
		return $this->deleteAll("comment_id in($comment_id)");
	}
	/**
	* 修改评论状态
	*/
	public function CommentUpdate($comment_id,$status)
    {
		return $this->updateAll(['comment_status' => $status],'comment_id=:comment',[':comment'=>$comment_id]);
	}

    /**
     * @inheritdoc 添加评论
     */
    public function CommentAdd($str)
    {
        return $this->find()
                ->select("find,replacement")
                ->from("edu_Illegal")
                ->where("instr('$str',find)")
                ->asarray()
                ->all();
    }


    /**
    * 量表联查
    */
    public function getUser()
    {
        return $this->hasMany(User::className(), ['user_id'=>'user_id']);
    }

}
